package com.jh.controller;
/**
 * 代号:隐无为 2017：厚溥
 * 文件名：NavsController.java
 * 创建人：柯栋
 * 日期：2017年9月21日
 * 修改人：
 * 描述：
 */


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.kd.common.base.controller.BaseController;
import org.kd.utils.JsonReturnData;
import org.kd.utils.PageTable;
import org.kd.utils.WebConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.jh.model.Classify;
import com.jh.model.Navs;
import com.jh.model.BTO.NavsBTO;
import com.jh.service.ClassifyService;
import com.jh.service.NavsService;

/**
 * 用途：业务模块名称
 */
@Controller
@RequestMapping("/navsController")
public class NavsController extends BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	NavsService navsService;
	@Autowired
	ClassifyService classifyService;
	@Value("${sys.webPath}")
	private String webPath;



	@RequestMapping("/list")
	public String list(ModelMap modelMap) {
		logger.info("==============="+this.getClass().getName()+"==================");
		return "navs/list";
	}

	@RequestMapping("/saveUpdatePage")
	public String saveUpdatePage(Integer id, ModelMap modelMap) {
		if (id != null) {
			modelMap.put("navs", navsService.queryById(id));
		}
		return "navs/saveUpdatePage";
	}

	@RequestMapping("/saveUpdate")
	@ResponseBody
	public String saveUpdate(Navs navs) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "操作失败");
		if (navs.getId() != null) {
			navsService.updateNoNull(navs);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("更新成功");
		} else {
			navs.setDelFlag(1);
			navsService.insert(navs);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("添加成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Navs navs) {

		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "删除失败");
		if (navs.getId() != null) {
			navs.setDelFlag(WebConstant.DELETECODE);
			navsService.updateDelete(navs);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/deletes")
	@ResponseBody
	public String deletes(String ids) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "批量删除失败");
		if (!StringUtils.isEmpty(ids)) {
//			navsService.updateDeletes(ids);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("批量删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/ajaxList")
	@ResponseBody
	public String ajaxList(Integer page, Integer limit) {
		PageTable<List<Navs>> pageTable = new PageTable<List<Navs>>();
		Navs u = new Navs();
		u.setDelFlag(WebConstant.NODELETECODE);
		PageInfo<Navs> pageInfo = navsService.queryPageListByParam(page, limit, u);
		pageTable.setCode(WebConstant.PAGESUCCESSCODE);
		pageTable.setMsg("获取成功");
		pageTable.setData(pageInfo.getList());
		pageTable.setCount(pageInfo.getTotal());
		return responseAPI.getJsonString(pageTable);
	}

	/**
	 *  @功能:组装菜单
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年4月29日
	 *  @return  
	 */
	@RequestMapping("/navs")
	@ResponseBody
	public String navs() {
		List<NavsBTO> list =  this.twoMenu(this.oneMenu());
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("contentManagement", list);
		return responseAPI.getJsonString(jsonMap);
	}
	

	/**
	 *  @param list 
	 * @功能:组装二级菜单
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年4月29日
	 *  @return  
	 */
	private List<NavsBTO> twoMenu(List<Navs> list) {
		List<NavsBTO> returnList = new ArrayList<NavsBTO>();
		Iterator<Navs> iterator = list.iterator();
		while (iterator.hasNext()) {
			NavsBTO navsBTO = new NavsBTO();
			Navs navs = (Navs) iterator.next();
			navs.setHref(webPath+navs.getHref());
			BeanUtils.copyProperties(navs,navsBTO);
			if(navs.getValue() != null && navs.getValue().trim().length() > 0){
				navsBTO.setChildren(navsService.queryByIds(navs.getValue()));
			}
			returnList.add(navsBTO);
		}
		return returnList;
	}



	/**
	 *  @功能:获取所有一级菜单
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年4月29日
	 *  @return  
	 */
	private List<Navs> oneMenu() {
		// TODO Auto-generated method stub
		Navs example = new Navs();
		example.setDelFlag(WebConstant.NODELETECODE);
		example.setSort(WebConstant.LEVEL_ONE_MENU);
		List<Navs> list = navsService.queryParamList(example );
		return list;
	}
}
